package com.mustafaiev.tair.cts.factory;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.mustafaiev.tair.cts.dao.IBasketDAO;
import com.mustafaiev.tair.cts.dao.IBasketsBatchDAO;
import com.mustafaiev.tair.cts.dao.IGroupDAO;
import com.mustafaiev.tair.cts.dao.IPayerDAO;
import com.mustafaiev.tair.cts.dao.IPurchaseDAO;
import com.mustafaiev.tair.cts.facade.BasketFacade;
import com.mustafaiev.tair.cts.facade.BasketsBatchFacade;
import com.mustafaiev.tair.cts.facade.GroupFacade;
import com.mustafaiev.tair.cts.facade.IBasketFacade;
import com.mustafaiev.tair.cts.facade.IBasketsBatchFacade;
import com.mustafaiev.tair.cts.facade.ICostTrackingSystemFacade;
import com.mustafaiev.tair.cts.facade.IGroupFacade;
import com.mustafaiev.tair.cts.facade.IPayerFacade;
import com.mustafaiev.tair.cts.facade.IPurchaseFacade;
import com.mustafaiev.tair.cts.facade.PayerFacade;
import com.mustafaiev.tair.cts.facade.PurchaseFacade;
import com.mustafaiev.tair.cts.service.DozerMapper;

public class CostTrackingSystemFacadeFactory implements
		ICostTrackingSystemFacadeFactory {

	private final Map<Class<?>, Object> facades = new HashMap<Class<?>, Object>();

	@Autowired
	private IPayerDAO payerDAO;

	@Autowired
	private IBasketDAO basketDao;

	@Autowired
	private IPurchaseDAO purchaseDao;

	@Autowired
	private IBasketsBatchDAO basketsBatchDao;

	@Autowired
	private IGroupDAO groupDao;

	private final DozerMapper mapper;

	private static final Logger LOGGER = Logger
			.getLogger(CostTrackingSystemFacadeFactory.class);

	public CostTrackingSystemFacadeFactory() {
		this.mapper = new DozerMapper();
	}

	@Override
	@SuppressWarnings("unchecked")
	public <T extends ICostTrackingSystemFacade> T getFacade(
			final Class<T> facade) {
		return (T) this.facades.get(facade);
	}

	/**
	 * Init method called by spring bean processor
	 */
	protected void initFacadeFactory() {
		createPayerFacade();
		createBasketFacade();
		createPurchaseFacade();
		createBasketsBatchFacade();
		createGroupFacade();
	}

	private void createPayerFacade() {
		final IPayerFacade facade = new PayerFacade(this.payerDAO,
				this.mapper.getMapper());
		this.facades.put(IPayerFacade.class, facade);
	}

	private void createBasketFacade() {
		final IBasketFacade facade = new BasketFacade(this.basketDao,
				this.mapper.getMapper());
		this.facades.put(IBasketFacade.class, facade);
	}

	private void createPurchaseFacade() {
		final IPurchaseFacade facade = new PurchaseFacade(this.purchaseDao,
				this.mapper.getMapper());
		this.facades.put(IPurchaseFacade.class, facade);
	}

	private void createBasketsBatchFacade() {
		final IBasketsBatchFacade facade = new BasketsBatchFacade(
				this.basketsBatchDao, this.mapper.getMapper());
		this.facades.put(IBasketsBatchFacade.class, facade);
	}

	private void createGroupFacade() {
		final IGroupFacade facade = new GroupFacade(this.groupDao,
				this.mapper.getMapper());
		this.facades.put(IGroupFacade.class, facade);
	}

}
